package com.igeek.dao;

import com.igeek.domain.Order;
import com.igeek.domain.OrderItem;
import com.igeek.utils.JDBCTools;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

/**
 * @author jerryHe
 * @create 2019-10-12 10:06
 */
public class OrderDao {
    QueryRunner qr = new QueryRunner();
    QueryRunner qrDs = new QueryRunner(JDBCTools.getDataSource());
    String sql = "";
    //事务操作，dao层使用throws
    public void saveOrder(Order order) throws SQLException {
        sql = "insert into orders values (?,now(),?,0,'','','',?)";
        qr.update(JDBCTools.threadLocal.get(),sql,
                order.getOid(),order.getTotal(),order.getUser().getUid());
    }

    public void saveOrderItem(OrderItem item) throws SQLException {
        sql ="insert into orderitem values (?,?,?,?,?)";
        qr.update(JDBCTools.threadLocal.get(),sql,
                item.getItemid(),item.getCount(),item.getSubtotal(),
                item.getProduct().getPid(),item.getOrder().getOid());
    }

    public void update(Order order) {
        sql = "update orders set address = ?,name=?,telephone =? where oid = ?" ;
        try {
            qrDs.update(sql,order.getAddress(),order.getName(),
                    order.getTelephone(),order.getOid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
